package com.kbbywyclient.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.kbbywyclient.R;
import com.kbbywyclient.app.Constants;
import com.kbbywyclient.data.entity.AccountInfo;
import com.kbbywyclient.data.entity.WithdrawRule;
import com.kbbywyclient.data.model.AccountInfoModel;
import com.kbbywyclient.data.model.MinePromotionModel;
import com.kbbywyclient.data.model.SepPercentModel;
import com.kbbywyclient.data.model.WithdrawRuleModel;
import com.kbbywyclient.presenter.base.BasePresenter;
import com.kbbywyclient.service.ApiService;
import com.kbbywyclient.service.ApiServiceHelper;
import com.kbbywyclient.ui.contarct.MinePromotionContract;
import com.kbbywyclient.util.HttpExcUtil;
import com.kbbywyclient.util.RsaJsonUtil;
import com.kbbywyclient.util.ToastUtil;
import com.kbbywyclient.util.pre.PreUtils;

import javax.inject.Inject;

import rx.Subscriber;

/**
 * Created by Jiangzx on 2017/9/11 16:41.
 */

public class MinePromotionPresenter extends BasePresenter implements MinePromotionContract.Presenter {
    @Inject
    Context mContext;
    @Inject
    ApiService apiService;
    @Inject
    RsaJsonUtil jsonUtil;

    private MinePromotionContract.View view;

    private double mini;
    private double max;

    @Inject
    public MinePromotionPresenter(MinePromotionContract.View view) {
        this.view = view;
    }

    @Override
    public void getPromotionData(int salesId) {
        jsonUtil.put("saleID", salesId);
        ApiServiceHelper.
                appointThreadNormal(apiService.getPromotionData(jsonUtil.getRsaParams())).
                subscribe(new Subscriber<MinePromotionModel>() {
                    @Override
                    public void onCompleted() {
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.closeLoadingDialog();
                        HttpExcUtil.printError(e);
                    }

                    @Override
                    public void onNext(MinePromotionModel model) {
                        if(model.getCode()==Constants.CODE_SUCCESS&&
                                model.getData()!=null){
                            view.setPromotionData(model.getData());
                        }else{
                            ToastUtil.showShort(model.getMsg());
                        }
                    }
                });
    }

    @Override
    public void getAccountInfo(int salesId) {
        jsonUtil.put("salesID", salesId);
        ApiServiceHelper.
                appointThreadNormal(apiService.getAccountInfo(jsonUtil.getRsaParams())).
                subscribe(new Subscriber<AccountInfoModel>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.closeLoadingDialog();
                        HttpExcUtil.printError(e);
                    }

                    @Override
                    public void onNext(AccountInfoModel accountInfoModel) {
                        view.closeLoadingDialog();
                        AccountInfo accountInfo = accountInfoModel.getData();
                        if (accountInfo != null) {
                            if (accountInfo.getBankcardBindStatus() == 1) {
                                //已绑卡
                                view.toWithdrawActivity(accountInfo, mini, max);
                            } else {
                                //未绑卡
                                view.showNotBindCardDialog();
                            }
                        }
                    }
                });
    }

    @Override
    public void getWithdrawRule() {
        view.showLoadingDialog(mContext.getString(R.string.loading));
        ApiServiceHelper.
                appointThreadNormal(apiService.getWithdrawRule(jsonUtil.getRsaParams())).
                subscribe(new Subscriber<WithdrawRuleModel>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        view.closeLoadingDialog();
                        HttpExcUtil.printError(e);
                    }

                    @Override
                    public void onNext(WithdrawRuleModel ruleModel) {
                        WithdrawRule rule = ruleModel.getData();
                        try {
                            String miniAmount=rule.getMinAmount();
                            if(!TextUtils.isEmpty(miniAmount)){
                                mini = Double.parseDouble(miniAmount);
                            }
                            String maxAmount=rule.getMaxAmount();
                            if(TextUtils.isEmpty(maxAmount)){
                                max=Double.MAX_VALUE;
                            }else{
                                max = Double.parseDouble(maxAmount);
                            }
                            getAccountInfo(PreUtils.getUser(mContext).getSalesID());
                        } catch (Exception e) {
                            HttpExcUtil.printError(e);
                            view.closeLoadingDialog();
                        }
                    }
                });
    }

    @Override
    public void getSepPercent(int salesId,final int action) {
        view.showLoadingDialog(mContext.getString(R.string.text_wait_a_moment));
        jsonUtil.put("salesID",salesId);
        ApiServiceHelper.
                appointThreadNormal(apiService.getSepPercent(jsonUtil.getRsaParams())).
                subscribe(new Subscriber<SepPercentModel>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        view.closeLoadingDialog();
                        HttpExcUtil.printError(e);
                    }

                    @Override
                    public void onNext(SepPercentModel sepPercentModel) {
                        view.closeLoadingDialog();
                        view.onSepPercentSuccess(sepPercentModel.getRatio(),action);
                    }
                });

    }
}
